package com.qc.sys.controller;


import com.qc.bus.service.BusGoodsService;
import com.qc.bus.service.BusProviderService;
import com.qc.sys.common.ActiveUser;
import com.qc.sys.common.DataGridView;
import com.qc.sys.entity.SysUser;
import com.qc.sys.service.SysUserService;
import com.qc.sys.service.UploadService;
import com.qc.sys.utils.UploadProperties;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("file")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private UploadProperties uploadProperties;

    @Autowired
    private SysUserService userService;


    @PostMapping("uploadFile")
    public Object uploadFile(MultipartFile mf){
        String path = this.uploadService.uploadImage(mf);
        String baseUrl = uploadProperties.getBaseUrl();
        Map<String,String> map  = new HashMap<>();
        map.put("src",path);
        //更新数据库
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        SysUser user = activeUser.getUser();
        user.setImgpath(path);
        userService.updateById(user);
        return new DataGridView(map);
    }

    @PostMapping("uploadGoodsFile")
    public Object uploadGoodsFile(MultipartFile mf){
        String path = this.uploadService.uploadImage(mf);
        Map<String,String> map  = new HashMap<>();
        map.put("src",path);
        return new DataGridView(map);
    }
}
